package com.tich.program.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tich.api.domain.dto.ProgramTicketAddDTO;
import com.tich.api.domain.dto.ProgramTicketDeductDTO;
import com.tich.api.domain.vo.ProgramClientSelectVO;
import com.tich.program.domain.entity.Program;
import com.tich.program.domain.entity.ProgramShowtime;
import com.tich.program.domain.entity.ProgramTicket;
import com.tich.program.mapper.ProgramMapper;
import com.tich.program.mapper.ProgramShowtimeMapper;
import com.tich.program.mapper.ProgramTicketMapper;
import com.tich.program.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ClientServiceImpl implements ClientService {

    @Autowired
    private ProgramMapper programMapper;
    @Autowired
    private ProgramTicketMapper programTicketMapper;
    @Autowired
    private ProgramShowtimeMapper programShowtimeMapper;

    /**
     * 根据id查询节目以及门票信息
     * @param programId
     * @param ticketId
     * @return
     * */
    public ProgramClientSelectVO programClientSelect(Long programId, Long ticketId) {
        //1.查询节目信息
        Program program = programMapper.selectById(programId);
        //2.查询节目时间信息
        ProgramShowtime programShowtime = programShowtimeMapper.selectById(programId);
        //3.查询门票信息
        ProgramTicket programTicket = programTicketMapper.selectById(ticketId);
        //4.封装返回类
        return ProgramClientSelectVO.builder()
                .title(program.getTitle())
                .poster(program.getPoster())
                .place(program.getPlace())
                .showDayTime(programShowtime.getShowDayTime())
                .remainingCount(programTicket.getRemainingCount())
                .price(programTicket.getPrice())
                .status(program.getStatus())
                .build();

    }

    /**
     * 扣减门票数量
     * @param programTicketDeductDTO
     * @return
     * */
    @Transactional
    public Boolean programTicketDeduct(ProgramTicketDeductDTO programTicketDeductDTO) {
        //当剩余票数大于等于购票数时，购票成功
        LambdaUpdateWrapper<ProgramTicket> programTicketLambdaUpdateWrapper = Wrappers
                .lambdaUpdate(ProgramTicket.class)
                .setSql("remaining_count = remaining_count - " + programTicketDeductDTO.getCount())
                .eq(ProgramTicket::getId, programTicketDeductDTO.getTicketId())
                .ge(ProgramTicket::getRemainingCount, programTicketDeductDTO.getCount());
        int update = programTicketMapper.update(programTicketLambdaUpdateWrapper);
        return update != 0;
    }

    /**
     * 取消订单，恢复门票库存
     * @param programTicketAddDTO
     * @return
     * */
    @Transactional
    public Boolean programTicketAdd(ProgramTicketAddDTO programTicketAddDTO) {
        ProgramTicket programTicket = programTicketMapper.selectById(programTicketAddDTO.getTicketId());
        LambdaUpdateWrapper<ProgramTicket> programTicketLambdaUpdateWrapper = Wrappers.lambdaUpdate(ProgramTicket.class)
                .set(ProgramTicket::getRemainingCount, programTicket.getRemainingCount() + programTicketAddDTO.getCount())
                .eq(ProgramTicket::getId, programTicketAddDTO.getTicketId());
        int update = programTicketMapper.update(programTicketLambdaUpdateWrapper);
        return update != 0;
    }

    /**
     * 查询门票种类
     * @param ticketId
     * @return
     */
    public String ticketCategorySelect(Long ticketId) {
        return programTicketMapper.ticketCategorySelect(ticketId);
    }
}
